# Copyright (c) 2009 by Alex Leone <acleone ~at~ gmail.com>
#
# This file is part of epp.
#
# epp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# epp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with epp.  If not, see <http://www.gnu.org/licenses/>.

r"""
Contains the [eval] tag.

Syntax:
    [eval s="python code to be eval-ed"]

Attributes:
    -- name: the tagname to store this tag as.  eg call the tag with
             `[tagname]'
             if tagname is `e' or `eval': eval and return value

Outputs:
    str(eval(value))

Examples:
    Note: s[1:] in p.parsetags(s[1:]) is to strip the leading '\n'
          from s

    >>> import epp
    >>> p = epp.Epp()
    >>> s = '''
    ... [eval s:]
    ...  5**2 == 25'''
    >>> print(p.parsetags(s[1:]))
    True
"""

import tags
from tags.tag import Tag

tagnames = ["eval"]

class Eval(Tag):

    def replace(self, eppinst, tagName, attrs):
        """
        The eval tag.  name="statements".
        See the python eval function.
        """
        if len(attrs) == 0:
            return ''
        return str(eval(eppinst.parsetags(attrs[0][1])))